<?php

class TestController extends Zend_Controller_Action {

    protected $_authNamespace;

    public function init() {
        /* Initialize action controller here */
        $this->_authNamespace = new Zend_Session_Namespace('Zend_Auth');
        $writer = new Zend_Log_Writer_Stream('../logs/test-de-nivel.log');
        $this->logger = new Zend_Log($writer);

        //$this->logger->info('Instantiating TestController');
    }

    public function indexAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $test = new Application_Model_TestMapper();
            $this->view->entries = $test->fetchAll();
        } else {
            return $this->_helper->redirector->gotoUrl('/auth/login');
        }
    }

    public function addTestAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $request = $this->getRequest();
            $form = new Application_Form_Test();

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $test = new Application_Model_Test($form->getValues());
                    $test->setName($form->getValue('name'));
                    $test->setLevelId($form->getValue('level_id'));
                    $mapper = new Application_Model_TestMapper();
                    $mapper->save($test);
                    return $this->_helper->redirector('index');
                }
            }

            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function editTestAction() {
        /**
         *  Shows the view to edit the Test.
         *  Shows the test form (just the name field) and a table with the list of its questionnaires.
         */
        if ($this->_authNamespace->userRole == 'admin') {
            $test_id = $this->_getParam("test_id");
            $request = $this->getRequest();
            $form = new Application_Form_Test();
            $this->view->test_id = $test_id;

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array("id" => (int) $test_id);

                    $test = new Application_Model_Test($data);
                    $mapper = new Application_Model_TestMapper();
                    $mapper->find($test_id, $test);
                    $test->setName($form->getValue('name'));
                    $test->setLevelId($form->getValue('level_id'));
                    $mapper->save($test);

                    return $this->_helper->redirector('index');
                }
            } else {
                $data = array("id" => (int) $test_id);

                $test = new Application_Model_Test($data);

                $mapper = new Application_Model_TestMapper();
                $mapper->find($test_id, $test);
                $form_data = array("id" => $test->getId(), "name" => $test->getName(), "level_id" => $test->getLevelId());
                $form->populate($form_data);

                $questionnaire = new Application_Model_QuestionnaireMapper();

                $this->view->allquestionnaires = $questionnaire->fetchAll();
                $this->view->questionnaires = $questionnaire->fetchByTest((int) $test_id);

                $this->view->level_id = $test->getLevelId();
            }


            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function addQuestionnaireAction() {
        /**
         *  Json view.
         * Adds a questionnaire to a Test.
         *  @return 'ok' if adds ok, 'fail' if there is any error.
         */
        if ($this->_authNamespace->userRole == 'admin') {
            if ($this->getRequest()->isPost()) {
                $request = $this->getRequest();
                if ($request->getParam('q_id') != '-1') {
                    try {

                        $t_id = $request->getParam('t_id');
                        $q_id = $request->getParam('q_id');


                        $tq = new Application_Model_TestQuestionnaire();
                        $tq->setTestId($t_id);
                        $tq->setQuestionnaireId($q_id);
                        $mapper = new Application_Model_TestQuestionnaireMapper();

                        $result = $mapper->fetchByQuestionnaireTest($q_id, $t_id);

                        if ($result == null) {
                            $mapper->save($tq);
                            $myResponse = array('ok');
                        } else {
                            $myResponse = array('repeated');
                        }
                    } catch (Exception $e) {
                        $myResponse = array('fail');
                    }
                } else {
                    $myResponse = array('no-selected');
                }

                $this->_helper->json($myResponse);
            }
        }
    }

    public function removeQuestionnaireAction() {
        /**
         *  Json view.
         * Removes a questionnaire from a Test.
         *  @return 'ok' if adds ok, 'fail' if there is any error.
         */
        if ($this->_authNamespace->userRole == 'admin') {

            $request = $this->getRequest();
            try {

                $t_id = $request->getParam('t_id');
                $q_id = $request->getParam('q_id');
                $tq = new Application_Model_TestQuestionnaire();
                $tq->setTestId($t_id);
                $tq->setQuestionnaireId($q_id);
                $mapper = new Application_Model_TestQuestionnaireMapper();
                $result = $mapper->fetchByQuestionnaireTest($q_id, $t_id);

                if ($result == null) {
                    $myResponse = 'fail';
                } else {
                    $tq->setId($result->id);
                    $mapper->delete($tq);
                    $myResponse = array('ok');
                }
            } catch (Exception $e) {
                $myResponse = array('fail');
            }

            $this->_helper->json($myResponse);
        }
    }

    public function deleteAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $test_id = $this->_getParam("test_id");
            $this->view->idValue = $test_id;
            if ($test_id != null) {
                $data = array("id" => (int) $test_id);

                $test = new Application_Model_Test($data);
                $mapper = new Application_Model_TestMapper();
                $mapper->delete($test);

                $go = $this->_getParam("go");

                if ($go == '' or $go == null) {
                    return $this->_helper->redirector('index');
                } else {
                    return $this->_helper->redirector->gotoUrl($go);
                }
            }
        } else {
            $this->view->message = "You don't have permissions to do this!";
            return $this->_helper->redirector('index');
        }
    }

}

